package com.Java.train.member.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.Java.train.common.context.LoginMemberContext;
import com.Java.train.common.exception.BusniessException;
import com.Java.train.common.exception.BusniessExceptionEnum;
import com.Java.train.common.response.PageResp;
import com.Java.train.common.response.CommonResp;
import com.Java.train.common.utils.SnowFlakeUtil;
import com.Java.train.member.domain.DTO.PassengerDTO;
import com.Java.train.member.domain.Passenger;
import com.Java.train.member.mapper.PassengerMapper;
import com.Java.train.member.req.PassengerQueryReq;
import com.Java.train.member.service.PassengerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PassengerServiceImpl extends ServiceImpl<PassengerMapper, Passenger> implements PassengerService {
    private static final Logger LOG = LoggerFactory.getLogger(PassengerService.class);
     /**
      * @Description: 保存或修改用户（集合保存和编辑乘车人功能）
      * @param Passenger
      * @return CommonResp<Object>
      */
    @Override
    public CommonResp<Object> savePassenger(Passenger passenger) {
        // 1.获取乘车人id，不存在则保存，存在则编辑
        Long id = passenger.getId();
        DateTime dateTime = DateTime.now();
        if(ObjectUtil.isNull(id)){
            passenger.setId(SnowFlakeUtil.getSnowFlakeNextId());
            passenger.setMemberId(LoginMemberContext.getId());
            passenger.setCreateTime(dateTime);
            passenger.setUpdateTime(dateTime);
            boolean save = save(passenger);
            return new CommonResp<>(save);
        } else {
            passenger.setUpdateTime(dateTime);
            boolean update = updateById(passenger);
            return new CommonResp<>(update);
        }
    }

    /**
      * @Description: 分页查询乘车人
      * @param PassengerQueryReq
      * @return  PageResp<PassengerDTO>
      */
    @Override
    public CommonResp<PageResp<PassengerDTO>> queryList(PassengerQueryReq passengerQueryReq) {
        // 1.获取当前用户id信息
        Long memberId = passengerQueryReq.getMemberId();
//        Long memberId = LoginMemberContext.getId();
        // 2.查询数据库中是否存在当前乘车人信息（memberId）
        LOG.info("查询页码：{}", passengerQueryReq.getPage());
        LOG.info("每页条数：{}", passengerQueryReq.getSize());
        PageHelper.startPage(passengerQueryReq.getPage(),passengerQueryReq.getSize());
        LambdaQueryWrapper<Passenger> wrapper = new LambdaQueryWrapper<>();
        List<Passenger> passengers = list(wrapper.eq(Passenger::getMemberId, memberId));
        if(passengers == null || CollUtil.isEmpty(passengers)){
            throw new BusniessException(BusniessExceptionEnum.MEMBER_PASSENGER_NOT_EXITS);
        }

        // 3 分析分页信息
        // 3.1 获取分页总条数和页数
        PageInfo<Passenger> pageInfo = new PageInfo<>(passengers);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        // 3.2 转为封装后的返回值并封装到分页返回值中
        List<PassengerDTO> list = BeanUtil.copyToList(passengers, PassengerDTO.class);

        PageResp<PassengerDTO> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        // 4.返回查询到的乘车人信息
        return new CommonResp<>(pageResp);
    }

     /**
      * @Description: 删除乘车人
      * @param id
      * @return  Common<boolean>()
      */
     @Override
     public CommonResp<Object> deleteById(Long id) {
         boolean isSuccess = removeById(id);
         return new CommonResp<>(isSuccess);
     }

    @Override
    public CommonResp<List<Passenger>> queryMine() {
         // 获取当前用户登录的id
        Long id = LoginMemberContext.getId();
        LambdaQueryWrapper<Passenger> wrapper = new LambdaQueryWrapper<>();
        List<Passenger> list = list(wrapper.eq(Passenger::getMemberId, id));
        return new CommonResp<>(list);
    }

}
